#include <stdio.h>
#include <event2/event.h>
#include <signal.h>
#include <stdlib.h>
#include <arpa/inet.h>

using namespace std;
void listen_cd(evutil_socket_t t, short s, void *arg)
{
}
int main()
{
    if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
    {
        perror("sinal");
        exit(1);
    }

    evutil_socket_t socketfd = socket(AF_INET, SOCK_STREAM, 0);
    if (socketfd == -1)
    {
        perror("socket");
        exit(-1);
    }
    evutil_make_socket_nonblocking(socketfd);
    evutil_make_listen_socket_reuseable(socketfd);
    sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8080);
    inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
    char ip[1024];
    inet_ntop(AF_INET, &addr.sin_addr, ip, sizeof(ip));
    printf("ip:%s\n", ip);
    if (bind(socketfd, (sockaddr *)&addr, sizeof(addr)) == -1)
    {
        perror("bind");
        exit(-1);
    }
    if (listen(socketfd, 10) == -1)
    {
        perror("listen");
        exit(-1);
    }
    event_base *base = event_base_new();

    event *ev = event_new(base, socketfd, EV_READ | EV_PERSIST, listen_cd, base);
    event_add(ev, NULL);
    event_base_dispatch(base);
    event_free(ev);
    event_base_free(base);
    return 0;
}